package com.fjt.book_trade.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.fjt.book_trade.dao.IProductDao;
import com.fjt.book_trade.pojo.Assess;
import com.fjt.book_trade.pojo.Product;

@Service("productservice")
public class ProductService {
	private IProductDao productDao;
	private Product product;
	
	public IProductDao getProductDao() {
		return productDao;
	}
	@Resource
	public void setProductDao(IProductDao productDao) {
		this.productDao = productDao;
	}
	public Product getProduct() {
		return product;
	}
	@Resource(name="product")
	public void setProduct(Product product) {
		this.product = product;
	}
	
	public Map<String, Object> getProductlist(){
		Map<String, Object> map=new HashMap<String, Object>();
		List<Product> list=productDao.selectAllProduct();
		if(list!=null){
			map.put("result", "success");
			map.put("productlist", list);
		}else{
			map.put("result", "fail");
		}
		return map;
	}
	
	public Map<String, Object> getMyProductlist(String username){
		Map<String, Object> map=new HashMap<String, Object>();
		List<Product> list=productDao.selectSellProduct(username);
		if(list!=null){
			map.put("result", "success");
			map.put("productlist", list);
		}else{
			map.put("result", "fail");
		}
		return map;
	}
	
	
	public Map<String, Object> getProductInfo(int product_id){
		Map<String, Object> map=new HashMap<String, Object>();
		Product product=productDao.findProductInfoById(product_id);
		if(product!=null){
			map.put("result", "success");
			map.put("ProductInfo", product);
		}else{
			map.put("result", "fail");
		}
		return map;
	}
	
	public boolean checkBuy(int product_id,int buynumber){//验证商品数量
		if(productDao.checknumber(product_id)>=buynumber){
			return true;
		}else{
			return false;
		}	
	}
	public Map<String,String> Buyproduct(int product_id,int buynumber){//买商品
		Map<String,String> map = new HashMap<String,String>();
		if(checkBuy(product_id, buynumber)){
			if(productDao.subProductnumber(buynumber, product_id))
				map.put("result", "success");	
			else
				map.put("result", "fail");
		}else
			map.put("result", "fail");
		return map;
	}
	
	public Map<String,String> BackProduct(int product_id,int buynumber){//退货
		Map<String,String> map = new HashMap<String,String>();
		if(checkBuy(product_id, buynumber)){
			if(productDao.addProductnumber(buynumber, product_id))
				map.put("result", "success");	
			else
				map.put("result", "fail");
		}else
			map.put("result", "fail");
		return map;
	}
	
	public Map<String,String> addProduct(Product product){//添加商品
		Map<String,String> map = new HashMap<String,String>();
//		int id = (int)(Math.random()*50000)+(int)(Math.random()*50000);
		if(productDao.findProductInfoById(product.getProduct_id())==null){
			productDao.addProduct(product);
			map.put("result", "success");
		}else
			map.put("result", "error");
		return map;
	}
	
	public Map<String,Object> modifyProductInfo(Product product){//更改商品信息
		Map<String,Object> map = new HashMap<String,Object>();
		if(productDao.modifyProduct(product)){
			map.put("result", "success");
		}else
			map.put("result", "error");
		return map;
	}
	
	public Map<String,Object> getProductAssess(int product_id){//获得商品评价
		Map<String,Object> map = new HashMap<String,Object>();
		List<Assess> list=productDao.getProductAssess(product_id);
		if(list!=null){
			map.put("result", "success");
			map.put("assesslist", list);
		}else
			map.put("result", "error");
		return map;
	}
	
	
//	public String uploadimage(MultipartFile file) throws IOException{//上传图片
//		String FILE_PATH = "/upload/"; 
//		String fileName = file.getOriginalFilename();  
//        File tempFile = new File(FILE_PATH, new Date().getTime() + String.valueOf(fileName));  
//        if (!tempFile.getParentFile().exists()) {  
//            tempFile.getParentFile().mkdir();  
//        } 
//        
//		if (!tempFile.exists()) {
//			tempFile.createNewFile();
//		}
//		file.transferTo(tempFile);
//		return FILE_PATH+tempFile.getName();
//	}
	
//	public void downloadimage(String path,HttpServletResponse response) throws IOException{//上传图片
//		OutputStream os = response.getOutputStream();  
//        try { 
//        	int length= "/upload/".length(); 
//        	String fileName=path.substring(length);
//            response.reset();  
//            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);  
//            response.setContentType("image/jpeg; charset=utf-8");  
//            os.write(FileUtils.readFileToByteArray(new File(path,fileName)));  
//            os.flush();  
//        } finally {  
//            if (os != null) {  
//                os.close();  
//            }  
//        }
//	}
}
